from django.conf import settings
from fdfs_client.client import Fdfs_client
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import SKUImage, SKU
from meiduo_admin.serializers.images import ImageSerializer, SKUSerializer
from meiduo_admin.serializers.user import PageNum


class ImageView(ModelViewSet):
    queryset = SKUImage.objects.all()
    serializer_class = ImageSerializer
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

    def simple(self,request):

        skus = SKU.objects.all()
        ser = SKUSerializer(skus,many=True)

        return Response(ser.data)
    # def create(self, request, *args, **kwargs):
    #     sku_id = request.data.get('sku')[0]
    #     data = request.FILES.get('image')
    #     # client = Fdfs_client(settings.FASTDFS_PATH)
    #     # res = client.upload_appender_by_buffer(data.read())
    #     # image_path = res['Remote file_id']
    #     # if res['Status']!='Upload successed.':
    #     #     return Response({'error':"上传图片失败"})
    #     # image = SKUImage.objects.create(sku_id = sku_id,image = image_path )
    #     data = request.data
    #     ser = self.get_serializer(data=data)
    #     ser.is_valid()
    #     ser.save()
    #     return Response(ser.data, status=201 )  # 前端需要接受201状态